package nl.grol.yakshop.util;

import java.text.DecimalFormat;
import java.util.logging.Logger;

/**
 * @author mgrol
 *
 */
public class ConversionUtil {
    private static final Logger LOGGER = Logger.getLogger(ConversionUtil.class.getName());

    private static final DecimalFormat DF_2 = new DecimalFormat("0.00");
	private static final DecimalFormat DF_3 = new DecimalFormat("0.000");

	public static String format2( double value) {
		return DF_2.format(value);
	}
	public static String format3( double value) {
		return DF_3.format(value);
	}
	
	public static Integer stringToNonNegativeInt( String in ) throws InvalidInputException {
		Integer value = null;
		
		if( in == null ) {
			throw new InvalidInputException("Missing integer input parameter");
		} else {
			try {
				value = Integer.parseInt(in);
				if( value .intValue() < 0 ) {
					throw new InvalidInputException("Invalid day parameter "+ in + ": cannot be negative");
				}
			} catch( NumberFormatException exc ) {
				
				LOGGER.warning(exc.getMessage() );

				throw new InvalidInputException("Invalid day parameter:"+ in );
			}
		}
			
		return value;
	}


}
